package com.xunk.scene.handler.equipment;

import com.xunk.common.core.text.Convert;
import com.xunk.common.core.utils.SpringUtils;
import com.xunk.common.core.utils.serialization.JsonSerializer;
import com.xunk.common.core.web.domain.AjaxResult;
import com.xunk.equipment.api.RemoteEquipmentService;
import com.xunk.scene.domain.SceneConditionEquipment;
import com.xunk.scene.handler.ConditionWrapper;
import com.xunk.scene.handler.DefaultConditionHandler;
import com.xunk.scene.handler.IConditionHandler;
import com.xunk.scene.util.ConditionUtil;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import javax.naming.OperationNotSupportedException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: HIM
 * @Package: com.xunk.scene.handler
 * @ClassName: ConditionEquipmentHandle
 * @Author: luanzhiwei
 * @Description:
 * @Date: 2021/5/11 10:40
 * @Version: 1.0
 */
@Slf4j
@NoArgsConstructor
public class ConditionEquipmentLeaf extends DefaultConditionHandler implements IConditionHandler, ConditionWrapper {
    SceneConditionEquipment conditionEquipment;

    boolean flag = false;

    public ConditionEquipmentLeaf(SceneConditionEquipment conditionEquipment) {
        this.conditionEquipment = conditionEquipment;
    }


    private RemoteEquipmentService remoteEquipmentService = SpringUtils.getBean(RemoteEquipmentService.class);


    /**
     * 判断条件是否满足
     *
     * @param oneMatchFlag true满足一条即可，false 需全部满足
     * @return
     * @throws OperationNotSupportedException
     */
    @Override
    public boolean match(boolean oneMatchFlag) {
        try {
            AjaxResult info = remoteEquipmentService.info(conditionEquipment.getDeviceSn());
            if (!info.isSuccess()) {
                log.info("设备属性查询失败");
                flag = false;
            }
            Object data = info.getData();
            if (ObjectUtils.isEmpty(data)) {
                log.info("设备属性查询失败,data为空");
                flag = false;
            }
            Map<String, Object> propertyMap = JsonSerializer.deserializeMap(JsonSerializer.serialize(data));
            String current = Convert.toStr(propertyMap.get(conditionEquipment.getDeviceProp()));
            String target = conditionEquipment.getPropValue();
            String compare = conditionEquipment.getCompare();
            if (ConditionUtil.compare(current, target, compare)) {
                flag = true;
            }
        } catch (Exception e) {
            log.error("设备属性条件判断报错");
            flag = false;
        } finally {
            log.info("设备条件id{}处理结果{}", conditionEquipment.getId(), flag);
            return flag;
        }
    }


    /**
     * 包装，将实体类封装为可操作的类型
     *
     * @param o
     * @param <T>
     * @return
     */
    @Override
    public <T> IConditionHandler wrap(T o) {
        return new ConditionEquipmentLeaf((SceneConditionEquipment) o);
    }

    @Override
    public <T> List<IConditionHandler> wrap(List<T> objects) {
        Iterator<T> iterator = objects.iterator();
        List<IConditionHandler> list = new ArrayList<>();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            list.add(new ConditionEquipmentLeaf((SceneConditionEquipment) next));
        }
        return list;
    }

}
